#ifdef WIN_PLATFORM
#include "PIHeaders.h"
#endif

#include "resource.h"
#include "MetaDataManager.h"

static const ASInt32 MAX_BUFLEN = 512;
static AVWindow gAVWindow = NULL;

// internal functions in this file
ASErr ASAPI SettingsDialogOnInit(ADMDialogRef dialog);

/** GUI function to show dialog and get user input back
 **/
bool MetadataDialog(MetadataManager &metadataManager)
{
	//Make sure resource chain is correct
	ADMUtils::InitializeADM();

	// Dispaly modal dialog to get user input
	sADMDialog->Modal(sADMPluginRef,
					"TASC:Extrakt",
					IDD_METADATA,
					kADMModalDialogStyle,
					SettingsDialogOnInit, 
					(ADMUserData) &metadataManager,
					NULL);

	ADMUtils::ReleaseADM();

	return true;
}

static void ASAPI OnH1Button(ADMItemRef itemRef, ADMNotifierRef notifierRef)
{
	ADMDialogRef dialogRef = sADMItem->GetDialog (itemRef);

	MetadataManager* metadataManager = (MetadataManager*) sADMDialog->GetUserData(dialogRef);

	ADMItemRef item = sADMDialog->GetItem(dialogRef, IDC_TITLE);
	sADMItem->SetTextW (item, ASTextGetUnicode(metadataManager->GetH1()));

	// Finally, we exhibit the default behavior.
	sADMItem->DefaultNotify (itemRef, notifierRef);
}

/** 
	Callback invoked when the OK button is selected.  This function is called on any notifications generated for the OK button.
	@param itemRef IN the OK button ADM item.
	@param notifierRef IN the event that triggered the call.
*/

static ASText GetControlText(ADMDialogRef dialogRef, ASInt32 itemId)
{
	ADMItemRef textEdit = sADMDialog->GetItem (dialogRef, itemId);
	ASInt32 textLen = sADMItem->GetTextLengthW (textEdit);	

	ADMUnicode msg[MAX_BUFLEN];
	sADMItem->GetTextW (textEdit, msg, MAX_BUFLEN);
	//pass in size in bytes for text
	return ASTextFromSizedUnicode(msg, kUTF16HostEndian, textLen*2);
}


static void ASAPI OnOkButton (ADMItemRef itemRef, ADMNotifierRef notifierRef)
{
	ADMDialogRef dialogRef = sADMItem->GetDialog (itemRef);

	MetadataManager* metadataManager = (MetadataManager *) sADMDialog->GetUserData(dialogRef);

	metadataManager->SetMeta(ASTextFromPDText("Title"),		GetControlText(dialogRef, IDC_TITLE));
	metadataManager->SetMeta(ASTextFromPDText("Author"),	GetControlText(dialogRef, IDC_AUTHOR));
	metadataManager->SetMeta(ASTextFromPDText("Keywords"),	GetControlText(dialogRef, IDC_KEYWORDS));

	// Finally, we exhibit the default behavior.
	sADMItem->DefaultNotify (itemRef, notifierRef);
}


/**
	Callback invoked when the dialog is destroyed. It is used to indicate an end to the dialog's modal behaviour.
	@param dialogRef IN a reference to the ADM dialog.
	@see AVAppEndModal
	@see AVWindowDestroy
*/
static void ASAPI OnDestroy (ADMDialogRef dialogRef)
{
	AVAppEndModal();
	if (NULL != gAVWindow)
		AVWindowDestroy (gAVWindow);
}


/* SetDialogDatabase
** -------------------------------------------------------------------- 
**
** Set up dialog item values
**/
ASErr ASAPI SetDialogDatabase(ADMDialogRef dialog)
{
	// get dialog's user data
 	MetadataManager* metadataManager = (MetadataManager *) sADMDialog->GetUserData(dialog);

	// set items' values
	ADMItemRef item;

	item = sADMDialog->GetItem(dialog, IDC_TITLE);
	sADMItem->SetTextW (item, ASTextGetUnicode(metadataManager->GetMeta(ASTextFromPDText("Title"))));
	
	item = sADMDialog->GetItem(dialog, IDC_AUTHOR);
	sADMItem->SetTextW (item, ASTextGetUnicode(metadataManager->GetMeta(ASTextFromPDText("Author"))));

	item = sADMDialog->GetItem(dialog, IDC_KEYWORDS);
	sADMItem->SetTextW (item, ASTextGetUnicode(metadataManager->GetMeta(ASTextFromPDText("Keywords"))));

	item = sADMDialog->GetItem(dialog, IDC_VERSION);
	sADMItem->SetTextW (item, ASTextGetUnicode(metadataManager->GetPDFVersion()));

	return kSPNoError;
}


static void ASAPI OnResetButton(ADMItemRef itemRef, ADMNotifierRef notifierRef)
{
	ADMDialogRef dialogRef = sADMItem->GetDialog (itemRef);

	SetDialogDatabase(dialogRef);


	// Finally, we exhibit the default behavior.
	sADMItem->DefaultNotify (itemRef, notifierRef);
}


/* SettingsDialogOnInit
** -------------------------------------------------------------------- 
**
** Set up initial values
*/
ASErr ASAPI SettingsDialogOnInit(ADMDialogRef dialogRef)
{
	ADMItemRef itemRef;

	// Insert an override for the OK button.
	itemRef = sADMDialog->GetItem (dialogRef, IDOK);
	sADMItem->SetNotifyProc (itemRef, OnOkButton);

	itemRef = sADMDialog->GetItem (dialogRef, IDC_H1);
	sADMItem->SetNotifyProc (itemRef, OnH1Button);

	itemRef = sADMDialog->GetItem (dialogRef, IDC_RESET);
	sADMItem->SetNotifyProc (itemRef, OnResetButton);

	/*
	// initialize items
	sADMDialog->SetDefaultItemID(dialog, IDC_SET);
	sADMDialog->SetCancelItemID(dialog, IDC_QUIT);

	ADMItemRef item;
	item = sADMDialog->GetItem(dialog, IDC_SET);
	sADMItem->SetNotifyProc(item, DoSetButton);

	item = sADMDialog->GetItem(dialog, IDC_GET);
	sADMItem->SetNotifyProc(item, DoGetButton);

	item = sADMDialog->GetItem(dialog, IDC_FLASH);
	sADMItem->SetNotifyProc(item, DoFlashButton);

	item = sADMDialog->GetItem(dialog, IDC_QUIT);
	sADMItem->SetNotifyProc(item, DoQuitButton);

#ifdef MAC_PLATFORM
	// get the rect of dummy control we created, then destroy dummy control
	item = sADMDialog->GetItem(dialog, IDC_SLIDER_FILLOPACITY);
	ASRect bounds;
	sADMItem->GetBoundsRect(item, &bounds); 
	sADMItem->Destroy(item);

	// create ADM scroll bar
	item = sADMDialog->CreateItem(dialog, IDC_SLIDER_FILLOPACITY, 
							kADMScrollbarType, &bounds, NULL, NULL, 0);
	sADMItem->SetNotifyProc(item, DoFillSlider);

	// get the rect of dummy control we created, then destroy dummy control
	item = sADMDialog->GetItem(dialog, IDC_SLIDER_STROKEOPACITY);
	sADMItem->GetBoundsRect(item, &bounds); 
	sADMItem->Destroy(item);

	// create ADM scroll bar
	item = sADMDialog->CreateItem(dialog, IDC_SLIDER_STROKEOPACITY, 
							kADMScrollbarType, &bounds, NULL, NULL, 0);
	sADMItem->SetNotifyProc(item, DoStrokeSlider);							
#else // WIN_PLATFORM
	item = sADMDialog->GetItem(dialog, IDC_SLIDER_FILLOPACITY);
	sADMItem->SetItemType(item, kADMSliderType);
	sADMItem->SetNotifyProc(item, DoFillSlider);

	item = sADMDialog->GetItem(dialog, IDC_SLIDER_STROKEOPACITY);
	sADMItem->SetItemType(item, kADMSliderType);
	sADMItem->SetNotifyProc(item, DoStrokeSlider);
	sADMDialog->SetNotifyProc(dialog, DoCloseButon);
#endif
*/
	// Tweaking required to behave correctly within Acrobat.
	sADMDialog->SetDestroyProc (dialogRef, OnDestroy);
	gAVWindow = AVWindowNewFromPlatformThing (AVWLmodal, AVWIN_WANTSKEY, NULL,
									gExtensionID, sADMDialog->GetWindowRef(dialogRef));
	AVAppBeginModal (gAVWindow);

	// set items' values
	return SetDialogDatabase(dialogRef);
}
